<?php

abstract class Veneer_Controller_Admin extends Zend_Controller_Action
{
/*	Variables
	array - jsZone
*/
    protected $_dbArray =array();
    protected $_moduleName = null;
    protected $_module_id = 0;
    protected $_config_module;
    protected $_primary_table;

	protected $_veneer_parms;	// array of parms
	protected $_veneer_log;	// array of parms
	protected $_site_config;
	protected $_msite_config;
	protected $_defaultAction = "view";	// array of parms
	protected $_auth;	// login
	protected $_dbAdapter;	// login
	protected $_registry;	// login
	protected $_debug;	// login

	function init()
	{
			// Load Module
		if (file_exists(SITE_PATH . 'moduleconfig/' . $this->_moduleName . '.ini')) {
			$this->view->module_config = new Zend_Config_Ini(SITE_PATH . 'moduleconfig/' . $this->_moduleName . '.ini', 'main');
		} else {
			$this->_redirect('/admin/'.$this->_moduleName.'/config/');
		}
		// Load variables from registry
		$this->_registry = Zend_Registry::getInstance();
		$this->_msite_config = $this->_registry->get('msite_config');
		$this->_site_config = $this->_registry->get('site_config');
		$this->_dbAdapter = $this->_registry->get('dbAdapter');
		$this->_veneer_log = $this->_registry->get('veneerLog');
		$this->_debug = $this->_registry->get('veneer_debug');
		$this->_session = new Veneer_Session();

		// load tables
		Veneer_Controller_Helper::loadModels($this->_dbArray);
		// load parameters
		$this->_veneer_parms = $this->_getAllParams();
		$this->_registry->set('veneer_parms', $this->_veneer_parms);

		//****set registry too for module name
		// configure view
		$this->view->addScriptPath(APP_PATH . 'Veneer/View/scripts/');
		$this->view->addHelperPath(APP_PATH. 'Veneer/View/Helper', 'Veneer_View_Helper');
		$this->view->baseUrl = $this->_request->getBaseUrl();
		$this->view->moduleUrl = $this->view->baseUrl . '/'.$this->_moduleName;

		if($this->_session->isLoggedIn())
			$this->view->loggedIn = true;
		else
			$this->view->loggedIn = false;

	}

 	function preDispatch()
	{//*******************************************************

/*		if($this->_msite_config->main->siteType == 'user') {
			if($this->_auth->hasIdentity()) {
//				if
			}
			else
			{
				//  so I can come back to it.
				$s = new Zend_Session_Namespace('login');
				$s->redirect = $_SERVER['REQUEST_URI'];
				// 10 minutes to login
				$s->setExpirationSeconds(600);

				$this->_redirect('http://www.icommunity.com');
			}
		}
		else
		{	// unsupported site type
				$this->_redirect('/error');
		}
*/
	}
    public function __call($method, $args)
    {
		// No action
    }
    public function configAction()
    {
		$path = SITE_PATH . 'moduleconfig/' . $this->_moduleName . '.ini';
   		if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
			print_r($_POST);
			$this->write_ini_file($path, $_POST);
			$this->_redirect("/admin/$this->_moduleName");
		} else {
			if (file_exists($path)) {
				$this->view->arr = parse_ini_file( $path);
			} else {
				$this->view->arr = array();
			}
		}

    }
    public function indexAction()
    {
		// no need to create function - just edit the view for menu options.
    }

/*
    public function __call($method, $args)
    {
		 if ('Action' == substr($method, -6)) {
            // If the action method was not found
			if(!file_exists(APP_PATH . 'modules/'.$this->_moduleName.'/views/scripts/index/'.substr($method, 0,-6).'.phtml')) {
				if(substr($method, 0,-6) == 'index') {
			 		$this->_redirect('/');
				} else {
					$path = '/'.$this->_moduleName;
			 		$this->_redirect($path);
				}
			}
        }
    }
*/
	protected function write_ini_file($path, $assoc_array) {
		$content = "[main]\n";
	    foreach ($assoc_array as $key => $item) {
			// is sub array
	        if (is_array($item)) {
	            $content .= "\n[$key]\n";
				$item2 = trim($item2);
	            foreach ($item as $key2 => $item2) {
	                $content .= "$key2 = \"$item2\"\n";
	            }
	        } else {
				$item = trim($item);
	            $content .= "$key = \"$item\"\n";
	        }
	    }
		$content .="[audit]\n";
        $content .= "userid = \"12\"\n";

	    if (!$handle = fopen($path, 'w')) {
	        return false;
	    }
	    if (!fwrite($handle, $content)) {
	        return false;
	    }

	    fclose($handle);
	    return true;
	}

}

